package com.buaa.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buaa.service.BaseService;
import com.buaa.service.InstitutionService;
import com.buaa.service.InstrumentService;
import com.buaa.service.ProjectService;
import com.buaa.service.TalentService;
import com.buaa.utils.JacksonUtil;

@Controller
@RequestMapping("relation")
public class SearchController {

	@Autowired
	private InstrumentService instrumentService;
	@Autowired
	private InstitutionService institutionService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private TalentService talentService;
	@Autowired
	private BaseService baseService;

	@RequestMapping("dxyq")
	public ModelAndView YqSearch(String id,Integer start) {
		System.out.println("yq:\t"+id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dxyqrelation");
		
		//id = "16";
		Map<String, Object> instrument = instrumentService.getInstrumentDetail(id);
		Map<String, Object> instituion = institutionService.getInstitutionByResource(id, "instrument");
		// 数据原因 --- 机构表中没有基地的id并且机构表中的基地名字和基地表中不符合 用的是机构所在基地
		// List<Object[]> base = baseService.getBaseByInstitution("2300020020094");
		List<Object[]> instruments = instrumentService.getRelationInstrument(id);

		mav.addObject("instrument", instrument);
		mav.addObject("instituion", instituion);
		mav.addObject("base", null);
		mav.addObject("instruments", instruments);
		return mav;
	}

	@RequestMapping("dw")
	public ModelAndView DwSearch(String id,Integer start) {
		System.out.println("dw:\t"+id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/dwrelation");
		start = (start == null)? 0 :start;
		//id = "2300020020094";
		Map<String, Object> instituion = institutionService.getInstitutionDetail(id);
		String id1 = "2300090010796";
		List<Object[]> instruments = instrumentService.getInstrumentByResource(id1, "institution");
//		List<Object[]> instruments = instrumentService.getInstrumentByResource(id, "institution");
		String id2 = "2300020030417";
		List<Object[]> projects = projectService.getProjectByInstitution(id2,start);
//		List<Object[]> projects = projectService.getProjectByInstitution(id);
		String id3 = "2300020020094";
		List<Object[]> talents = talentService.getTalentByResource(id3, "institution");
		List<Object[]> bases = baseService.getBaseByInstitution(id3,start);
//		List<Object[]> talents = talentService.getTalentByResource(id, "institution");
//		List<Object[]> bases = baseService.getBaseByInstitution(id);

		mav.addObject("id", id);
		mav.addObject("instituion", instituion);
		mav.addObject("instruments", instruments);
		mav.addObject("projects", projects);
		mav.addObject("talents", talents);
		mav.addObject("bases", bases);
		return mav;
	}
	
	@RequestMapping("dw_add")
	@ResponseBody
	public String DwAddSearch(String id,String resource,Integer start) {
		System.out.println("dw_add:\t"+id);
		switch (resource) {
		case "project":
			String id2 = "2300020030417";
			List<Object[]> projects = projectService.getProjectByInstitution(id2,start);	
			return JacksonUtil.toJSon(projects);
		case "base":
			String id3 = "2300020020094";
			List<Object[]> bases = baseService.getBaseByInstitution(id3,start);	
			return JacksonUtil.toJSon(bases);
		default:
			break;
		}


		return null;
	}

	@RequestMapping("xm")
	public ModelAndView XmSearch(String id,Integer start) {
		System.out.println("xm:\t"+id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/xmrelation");
		
		//id = "9501";
		Map<String, Object> project = projectService.getProjectDetail(id);
		Map<String, Object> instituion = institutionService.getInstitutionByResource(id, "project");
		List<Object[]> projects = projectService.getRelationProject(id);

		mav.addObject("project", project);
		mav.addObject("instituion", instituion);
		mav.addObject("projects", projects);

		return mav;
	}

	@RequestMapping("ry")
	public ModelAndView RySearch(String id,Integer start) {
		System.out.println("ry:\t"+id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/ryrelation");
		
		//id = "7";
		Map<String, Object> talent = talentService.getTalentDetail(id);
		Map<String, Object> instituion = institutionService.getInstitutionByResource(id, "talent");
		Map<String, Object> base = baseService.getBaseByTalent(id);
		List<Object[]> talents = talentService.getRelationTalent(id);

		mav.addObject("talent", talent);
		mav.addObject("instituion", instituion);
		mav.addObject("base", base);
		mav.addObject("talents", talents);

		return mav;
	}

	@RequestMapping("syjd")
	public ModelAndView SyjdSearch(String id,Integer start) {
		System.out.println("syjd:\t"+id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hljlayer/syjdrelation");
		
		//id = "4";
		Map<String, Object> base = baseService.getBaseDetail(id);
		Map<String, Object> instituion = institutionService.getInstitutionByResource(id, "base");
		// 因为仪器表中的基地没有id，且名字对不上 此处用的是institution的仪器
		List<Object[]> instruments = instrumentService.getInstrumentByResource("2300090010796", "base");
		List<Object[]> talents = talentService.getTalentByResource(id, "base");

		mav.addObject("base", base);
		mav.addObject("instituion", instituion);
		mav.addObject("instruments", instruments);
		mav.addObject("talents", talents);

		return mav;
	}
}
